package cn.bluedot.framework.service;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import cn.bluedot.framework.annotations.RequestView;
import cn.bluedot.framework.exception.ServiceException;
import cn.bluedot.framework.web.HandlerManager;
import cn.bluedot.framework.web.RequestHandler;
import cn.bluedot.tea.common.QueryService;

public class BaseService {
	
	private static final String QUERY_PATTERN = "/query/.+";

	public BaseService() {}
	
	public BaseService(Map<String,Object[]> params, Map<String, Object> data) {}
	
	/**
	 * 根据对应RequestHandler实例化对应Service
	 * @param service
	 * @param params
	 * @param data
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static void creatService(RequestHandler service, Map<String, Object[]> params, Map<String, Object> data) {
					
		try {
			
			String uri = service.getUri();
			Class<?> clazz = null;
			if(Pattern.matches(QUERY_PATTERN, uri)) {
				clazz = (Class<QueryService>) QueryService.class;
			}else {
				clazz = (Class<BaseService>) service.getService();
			}
								
			Constructor<BaseService> constructor = (Constructor<BaseService>) clazz.getConstructor(String.class, Map.class, Map.class);
				
			constructor.newInstance(uri, params, data);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	/**
	 * 	子类调用业务逻辑
	 * @param params 传入参数
	 * @param data 调用后数据
	 * @throws Exception 
	 */
	protected void execute(String uri, Map<String, Object[]> params, Map<String, Object> data) {
		
		Method method = getMethod(uri);
		
		if(method == null) {
			try {
				throw new ServiceException("method is not found");
			} catch (ServiceException e) {
				e.printStackTrace();
			}
		}
		
		if(method.isAnnotationPresent(RequestView.class)) { //存入当前uri对应的view
			String view = method.getDeclaredAnnotation(RequestView.class).view();
			if(view == null) {
				view = uri.replaceFirst("/query/", "");			
			}
			Object[] value = new Object[] {view};
			params.put("view", value);
		}
		
        try {
        	method.setAccessible(true);
			method.invoke(this, params, data);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
		}		
	}

	private Method getMethod(String uri) {
		Method method = null;
		if(Pattern.matches(QUERY_PATTERN, uri)) {//匹配到查询
			try {
				Class<QueryService> clazz = QueryService.class;
				method = clazz.getMethod("exectueQuery", Map.class, Map.class);
			} catch (NoSuchMethodException | SecurityException e) {
				e.printStackTrace();
			}
		} else {
			method = HandlerManager.getMethod(uri); //此处需要调用HandlerManager
		}
		return method;
	}
	

	protected Map<String, Object> getParameterMap(Map<String, Object[]> params) {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		for (Entry<String, Object[]> entry : params.entrySet()) {
			map.put(entry.getKey(), entry.getValue()[0]);
        }
		
		return map;
	}
	
	
	
}
